package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分类管理
 */

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增分类
     * @param category
     * @param request
     * @return
     */
    @PostMapping
    public R sava(@RequestBody Category category, HttpServletRequest request){

/*        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        //新增分类的用户就是登录的用户
        Long empId = (long)request.getSession().getAttribute("employee");
        category.setCreateUser(empId);
        category.setUpdateUser(empId);*/

        categoryService.save(category);

        return R.success("新增分类成功");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize){

        //分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加条件，根据sort升序排序
        lqw.orderByAsc(Category::getSort);

        //分页查询调page方法，需要两个参数，分页构造器和条件构造器
        categoryService.page(pageInfo,lqw);
        return R.success(pageInfo);

    }


    /**
     * 根据id删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(Long id){


        R r = categoryService.deleteById(id);
        return r;

    }




    /**
     * 根据id修改分类信息
     * @param category
     * @param request
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category, HttpServletRequest request){

/*        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Long empId = (long)request.getSession().getAttribute("employee");
        category.setCreateUser(empId);
        category.setUpdateUser(empId);*/

        categoryService.updateById(category);
        return R.success("修改分类成功");
    }


    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){

        // select * from category where type=1
        // order by sort desc

        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        lambdaQueryWrapper.orderByDesc(Category::getSort);

        List<Category> list = categoryService.list(lambdaQueryWrapper);



        return R.success(list);

    }



}
